/**
 * 系统服务 - 封装系统相关的IPC调用
 * 提供系统信息、设置管理、文件操作等功能
 */

/**
 * 系统服务类
 */
export class SystemService {
  /**
   * 使用系统默认程序打开文件
   * @param filePath 文件路径
   * @returns 操作结果
   */
  static async openFile(filePath: string): Promise<void> {
    const response = await window.electronAPI.system.openFile({ filePath })
    
    if (!response.success) {
      throw new Error(response.error || '打开文件失败')
    }
  }

  /**
   * 在文件夹中显示文件
   * @param filePath 文件路径
   * @returns 操作结果
   */
  static async showInFolder(filePath: string): Promise<void> {
    const response = await window.electronAPI.system.showInFolder({ filePath })
    
    if (!response.success) {
      throw new Error(response.error || '在文件夹中显示文件失败')
    }
  }

  /**
   * 获取应用版本
   * @returns 应用版本号
   */
  static async getAppVersion(): Promise<string> {
    const response = await window.electronAPI.system.getAppVersion()
    
    if (!response.success) {
      throw new Error(response.error || '获取应用版本失败')
    }
    
    return response.data
  }

  /**
   * 获取系统信息
   * @returns 系统信息对象
   */
  static async getSystemInfo(): Promise<{
    appVersion: string;
    electronVersion: string;
    nodeVersion: string;
    chromeVersion: string;
    platform: string;
    arch: string;
  }> {
    const response = await window.electronAPI.system.getSystemInfo()
    
    if (!response.success) {
      throw new Error(response.error || '获取系统信息失败')
    }
    
    return response.data
  }

  /**
   * 获取设置
   * @returns 设置对象
   */
  static async getSettings(): Promise<any> {
    const response = await window.electronAPI.system.getSettings()
    
    if (!response.success) {
      throw new Error(response.error || '获取设置失败')
    }
    
    return response.data
  }

  /**
   * 保存设置
   * @param settings 设置对象
   * @returns 操作结果
   */
  static async saveSettings(settings: any): Promise<void> {
    const response = await window.electronAPI.system.saveSettings(settings)
    
    if (!response.success) {
      throw new Error(response.error || '保存设置失败')
    }
  }

  /**
   * 重置设置为默认值
   * @returns 操作结果
   */
  static async resetSettings(): Promise<void> {
    const response = await window.electronAPI.system.resetSettings()
    
    if (!response.success) {
      throw new Error(response.error || '重置设置失败')
    }
  }

  /**
   * 清理缓存
   * @returns 操作结果
   */
  static async clearCache(): Promise<{ success: boolean; message: string }> {
    const response = await window.electronAPI.system.clearCache()
    
    if (!response.success) {
      throw new Error(response.error || '清理缓存失败')
    }
    
    return response.data
  }

  /**
   * 导出数据
   * @returns 导出结果
   */
  static async exportData(): Promise<{ success: boolean; path?: string; message: string }> {
    const response = await window.electronAPI.system.exportData()
    
    if (!response.success) {
      throw new Error(response.error || '导出数据失败')
    }
    
    return response.data
  }

  /**
   * 选择目录
   * @returns 选择的目录路径
   */
  static async selectDirectory(): Promise<string | null> {
    const response = await window.electronAPI.system.selectDirectory()
    
    if (!response.success) {
      throw new Error(response.error || '选择目录失败')
    }
    
    return response.data
  }

  /**
   * 检查更新
   * @returns 更新信息
   */
  static async checkForUpdates(): Promise<{
    hasUpdate: boolean;
    version?: string;
    downloadUrl?: string;
    releaseNotes?: string;
  }> {
    const response = await window.electronAPI.system.checkForUpdates()
    
    if (!response.success) {
      throw new Error(response.error || '检查更新失败')
    }
    
    return response.data
  }

  /**
   * 下载并安装更新
   * @returns 操作结果
   */
  static async downloadAndInstallUpdate(): Promise<void> {
    const response = await window.electronAPI.system.downloadAndInstallUpdate()
    
    if (!response.success) {
      throw new Error(response.error || '下载并安装更新失败')
    }
  }

  /**
   * 获取数据库信息
   * @returns 数据库信息
   */
  static async getDatabaseInfo(): Promise<{
    path: string;
    size: string;
    photoCount: number;
    albumCount: number;
    tagCount: number;
  }> {
    const response = await window.electronAPI.system.getDatabaseInfo()
    
    if (!response.success) {
      throw new Error(response.error || '获取数据库信息失败')
    }
    
    return response.data
  }

  /**
   * 优化数据库
   * @returns 操作结果
   */
  static async optimizeDatabase(): Promise<{ success: boolean; message: string }> {
    const response = await window.electronAPI.system.optimizeDatabase()
    
    if (!response.success) {
      throw new Error(response.error || '优化数据库失败')
    }
    
    return response.data
  }

  /**
   * 清理数据库
   * @returns 操作结果
   */
  static async vacuumDatabase(): Promise<{ success: boolean; message: string }> {
    const response = await window.electronAPI.system.vacuumDatabase()
    
    if (!response.success) {
      throw new Error(response.error || '清理数据库失败')
    }
    
    return response.data
  }

  /**
   * 重置数据库
   * @returns 操作结果
   */
  static async resetDatabase(): Promise<{ success: boolean; message: string }> {
    const response = await window.electronAPI.system.resetDatabase()
    
    if (!response.success) {
      throw new Error(response.error || '重置数据库失败')
    }
    
    return response.data
  }

  /**
   * 设置开机自启动
   * @param enabled 是否启用
   * @returns 操作结果
   */
  static async setAutoStart(enabled: boolean): Promise<{ success: boolean; message: string }> {
    const response = await window.electronAPI.system.setAutoStart(enabled)
    
    if (!response.success) {
      throw new Error(response.error || '设置开机自启动失败')
    }
    
    return response.data
  }

  /**
   * 获取开机自启动状态
   * @returns 是否启用自启动
   */
  static async getAutoStartStatus(): Promise<boolean> {
    const response = await window.electronAPI.system.getAutoStartStatus()
    
    if (!response.success) {
      throw new Error(response.error || '获取开机自启动状态失败')
    }
    
    return response.data
  }

  /**
   * 设置自动导入文件夹
   * @param folderPath 文件夹路径
   * @returns 操作结果
   */
  static async setAutoImportFolder(folderPath: string): Promise<{ success: boolean; message: string }> {
    const response = await window.electronAPI.system.setAutoImportFolder(folderPath)
    
    if (!response.success) {
      throw new Error(response.error || '设置自动导入文件夹失败')
    }
    
    return response.data
  }

  /**
   * 获取自动导入文件夹
   * @returns 文件夹路径
   */
  static async getAutoImportFolder(): Promise<string | null> {
    const response = await window.electronAPI.system.getAutoImportFolder()
    
    if (!response.success) {
      throw new Error(response.error || '获取自动导入文件夹失败')
    }
    
    return response.data
  }

  /**
   * 选择文件夹
   * @returns 选择的文件夹路径
   */
  static async selectFolder(): Promise<string> {
    const response = await window.electronAPI.system.selectFolder()
    
    if (!response.success) {
      throw new Error(response.error || '选择文件夹失败')
    }
    
    return response.data
  }

  /**
   * 选择文件
   * @param options 选择选项
   * @returns 选择的文件路径或路径数组
   */
  static async selectFile(options?: {
    filters?: Array<{ name: string; extensions: string[] }>
    multiple?: boolean
  }): Promise<string | string[]> {
    const response = await window.electronAPI.system.selectFile(options || {})
    
    if (!response.success) {
      throw new Error(response.error || '选择文件失败')
    }
    
    return response.data
  }

  /**
   * 获取用户数据目录
   * @returns 用户数据目录路径
   */
  static async getUserDataPath(): Promise<string> {
    const response = await window.electronAPI.system.getUserDataPath()
    
    if (!response.success) {
      throw new Error(response.error || '获取用户数据目录失败')
    }
    
    return response.data
  }

  /**
   * 获取临时目录
   * @returns 临时目录路径
   */
  static async getTempPath(): Promise<string> {
    const response = await window.electronAPI.system.getTempPath()
    
    if (!response.success) {
      throw new Error(response.error || '获取临时目录失败')
    }
    
    return response.data
  }

  /**
   * 获取文档目录
   * @returns 文档目录路径
   */
  static async getDocumentsPath(): Promise<string> {
    const response = await window.electronAPI.system.getDocumentsPath()
    
    if (!response.success) {
      throw new Error(response.error || '获取文档目录失败')
    }
    
    return response.data
  }

  /**
   * 获取图片目录
   * @returns 图片目录路径
   */
  static async getPicturesPath(): Promise<string> {
    const response = await window.electronAPI.system.getPicturesPath()
    
    if (!response.success) {
      throw new Error(response.error || '获取图片目录失败')
    }
    
    return response.data
  }

  /**
   * 显示消息框
   * @param options 消息框选项
   * @returns 用户选择的按钮索引
   */
  static async showMessageBox(options: {
    type?: 'none' | 'info' | 'error' | 'question' | 'warning'
    title: string
    message: string
    buttons?: string[]
  }): Promise<number> {
    const response = await window.electronAPI.system.showMessageBox(options)
    
    if (!response.success) {
      throw new Error(response.error || '显示消息框失败')
    }
    
    return response.data
  }

  /**
   * 监听窗口最小化事件
   * @param callback 事件回调
   */
  static onWindowMinimize(callback: () => void): void {
    window.electronAPI.on('window:minimize', callback)
  }

  /**
   * 监听窗口最大化事件
   * @param callback 事件回调
   */
  static onWindowMaximize(callback: () => void): void {
    window.electronAPI.on('window:maximize', callback)
  }

  /**
   * 监听窗口关闭事件
   * @param callback 事件回调
   */
  static onWindowClose(callback: () => void): void {
    window.electronAPI.on('window:close', callback)
  }

  /**
   * 监听应用更新可用事件
   * @param callback 事件回调
   */
  static onUpdateAvailable(callback: (event: any, info: any) => void): void {
    window.electronAPI.on('app:update-available', callback)
  }

  /**
   * 监听系统事件
   * @param callback 事件回调
   */
  static onSystemEvent(callback: (event: string, data: any) => void): void {
    window.electronAPI.on('system:event', callback)
  }

  /**
   * 移除所有事件监听
   * @param channel 事件频道
   */
  static removeAllListeners(channel: string): void {
    window.electronAPI.removeAllListeners(channel)
  }
}

// 导出默认实例，方便使用
export default SystemService